package com.roc.web.action;

import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;
import com.roc.util.constant.Role;

public class OperatorQuerySearchCondition extends BaseSearchCondition {

	private String orgCode;
	private String opId;//当前操作员id
	private String currrentRoleType;//当前操作员角色类型
	private String roleType;
	private String accountNo;
	private String realName;
	private String mobile;
	private String cardNo;
	private String org_code;
	
	private String[] agentId;//所属代理商
	

	
	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		// TODO Auto-generated method stub
	    	 List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();
			if (Constance.isNotEmpty(accountNo)) {
				filedBeanList.add(new WhereHQLFieldBean("T.ACCOUNT_NO", accountNo.trim(), "=", false));
			}
			if (Constance.isNotEmpty(realName)) {
				filedBeanList.add(new WhereHQLFieldBean("T.REAL_NAME", realName.trim(),
						"LIKE", false, "ALL"));
			}
			if (Constance.isNotEmpty(mobile)) {
				filedBeanList.add(new WhereHQLFieldBean("T.MOBILE", mobile.trim(), "=", false));
			}
			if (Constance.isNotEmpty(cardNo)) {
				filedBeanList.add(new WhereHQLFieldBean("T.CARD_NO", cardNo.trim(), "=", false));
			}
			if (Constance.isNotEmpty(orgCode)&&!"99".equals(orgCode)) {
					filedBeanList.add(new WhereHQLFieldBean("T.ORG_CODE", orgCode.trim(), "=", false));
			}
			if (Constance.isNotEmpty(roleType)&&!"99".equals(roleType)) {
				filedBeanList.add(new WhereHQLFieldBean("T.ROLE_TYPE", roleType.trim(), "=", false));
		    }
			
			/*if(agentId!=null&&agentId.length >0){//代理商
	        	filedBeanList.add(new WhereHQLFieldBean("o.id IN("+Constance.getArrayContent(agentId)+")"));
	        }*/

			return filedBeanList;
	}

	@Override
	protected String getRowCountHQL() {
		StringBuffer  sb= new StringBuffer();
		if(!currrentRoleType.trim().equals("00")){
			if(currrentRoleType.trim().equals(Role.O_MANAGER.getCode())||currrentRoleType.trim().equals(Role.CLEAR_OPERATORS.getCode())||currrentRoleType.trim().equals(Role.CLEAR_LEAD.getCode())||currrentRoleType.trim().equals(Role.OPERATORS_COMM.getCode())||currrentRoleType.trim().equals(Role.OPERATORS_LEAD.getCode())||currrentRoleType.trim().equals(Role.RISK_CONTROL_OPERATORS.getCode())||currrentRoleType.trim().equals(Role.RISK_CONTROL_LEAD.getCode())){
				if(org_code.equals("48502000")){
					sb.append(" SELECT COUNT(1)");
					sb.append(" FROM T_OPERATORS T, TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD and T.role_type <> 15 AND TI.INS_TYPE = '1'");
					sb.append(" AND T.STATUS IN('00','02')");
				}else{
					sb.append(" select count(1) from T_OPERATORS t where t.ONEAGENTID=")
					.append(org_code);
				}
			}else if(currrentRoleType.trim().equals(Role.MARKET_DIRECT.getCode())||currrentRoleType.trim().equals(Role.MARKET_LEAD.getCode())){
				sb.append(" select count(1) from T_OPERATORS t where T.role_type <> 15 and t.ONEAGENTID in ")
				.append("(select t.agentid from t_oper_role t where t.operid=")
				.append(opId+")");
			}else{
				sb.append(" SELECT COUNT(1) FROM (");
				sb.append(" SELECT T.*, TI.INS_NAME FROM T_OPERATORS T, TBL_INS_INF TI ");
				sb.append("  WHERE T.ORG_CODE = TI.INS_ID_CD and T.role_type <> 15 AND TI.INS_TYPE = '1' AND T.STATUS IN ('00', '02')");
				sb.append(" START WITH ID =").append(opId);
				sb.append(" CONNECT BY PRIOR T.ID = T.SUPRI_ID )T");
				sb.append(" WHERE 1=1 ");
			}
		}else{//系统管理员
			sb.append(" SELECT COUNT(1)");
			sb.append(" FROM T_OPERATORS T, TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD and T.role_type <> 15 AND TI.INS_TYPE = '1'");
			sb.append(" AND T.STATUS IN('00','02')");
		}
		return sb.toString();
		/*return "select count(*) from Operators o where 1=1 and o.status in('00','02') and o.roleType<>'00' ";*/
	}

	@Override
	protected String getSearchHQL() {
		StringBuffer  sb= new StringBuffer();
		if(!currrentRoleType.trim().equals("00")){
			if(currrentRoleType.trim().equals(Role.O_MANAGER.getCode())||currrentRoleType.trim().equals(Role.CLEAR_OPERATORS.getCode())||currrentRoleType.trim().equals(Role.CLEAR_LEAD.getCode())||currrentRoleType.trim().equals(Role.OPERATORS_COMM.getCode())||currrentRoleType.trim().equals(Role.OPERATORS_LEAD.getCode())||currrentRoleType.trim().equals(Role.RISK_CONTROL_OPERATORS.getCode())||currrentRoleType.trim().equals(Role.RISK_CONTROL_LEAD.getCode())){
				if(org_code.equals("48502000")){
					sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
					sb.append(" T.CARD_NO, T.STATUS,TI.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
					sb.append(" FROM T_OPERATORS T, TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD and T.role_type <> 15 AND TI.INS_TYPE = '1'");
					sb.append(" AND T.STATUS IN('00','02')");
				}else{
					sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
					sb.append(" T.CARD_NO, T.STATUS,TI.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
					sb.append(" from T_OPERATORS t ,TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD and T.role_type <> 15 AND TI.INS_TYPE = '1' and t.ONEAGENTID=")
					.append(org_code);
				}
			}else if(currrentRoleType.trim().equals(Role.MARKET_DIRECT.getCode())||currrentRoleType.trim().equals(Role.MARKET_LEAD.getCode())){
				sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
				sb.append(" T.CARD_NO, T.STATUS,TI.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
				sb.append(" from T_OPERATORS t ,TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD and T.role_type <> 15 AND TI.INS_TYPE = '1' and t.ONEAGENTID in ")
				.append("(select t.agentid from t_oper_role t where t.operid=")
				.append(opId+")");
			}else{
				sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
				sb.append(" T.CARD_NO, T.STATUS,T.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
				sb.append(" FROM (SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,T.CARD_NO, T.STATUS,");
				sb.append(" T.VALID_START_DATE,T.VALID_END_DATE,T.DT_CREATE,TI.INS_NAME,T.ORG_CODE FROM T_OPERATORS T, TBL_INS_INF TI ");
				sb.append("  WHERE T.ORG_CODE = TI.INS_ID_CD and T.role_type <> 15 AND TI.INS_TYPE = '1' AND T.STATUS IN ('00', '02')");
				sb.append(" START WITH ID =").append(opId);
				sb.append(" CONNECT BY PRIOR T.ID = T.SUPRI_ID )T");
				sb.append(" WHERE 1=1 ");
			}
			
		}else{//系统管理员
			sb.append(" SELECT T.ID, T.ACCOUNT_NO,T.REAL_NAME,T.SEX, T.MOBILE,T.ROLE_TYPE,");
			sb.append(" T.CARD_NO, T.STATUS,TI.INS_NAME,T.VALID_START_DATE,T.VALID_END_DATE");
			sb.append(" FROM T_OPERATORS T, TBL_INS_INF TI WHERE T.ORG_CODE = TI.INS_ID_CD AND TI.INS_TYPE = '1' AND T.role_type <> 15");
			sb.append(" AND T.STATUS IN('00','02')");
		}
		return sb.toString();
	}

	@Override
	protected String getOrderPartOfHQL() {
		return " ORDER BY T.DT_CREATE ";
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public String getAccountNo() {
		return accountNo;
	}

	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public String[] getAgentId() {
		return agentId;
	}

	public void setAgentId(String[] agentId) {
		this.agentId = agentId;
	}

	public String getOpId() {
		return opId;
	}

	public void setOpId(String opId) {
		this.opId = opId;
	}

	public String getCurrrentRoleType() {
		return currrentRoleType;
	}

	public void setCurrrentRoleType(String currrentRoleType) {
		this.currrentRoleType = currrentRoleType;
	}

	public String getOrg_code() {
		return org_code;
	}

	public void setOrg_code(String org_code) {
		this.org_code = org_code;
	}

}
